package com.jsp.servlet; /**
 * @author wzm
 * @create 2023-03-10 10:44
 */

import com.jsp.entity.Employee;
import com.jsp.service.EmployeeService;
import com.jsp.service.impl.EmployeeServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/getAll")
public class ListServlet extends HttpServlet {
    private EmployeeService employeeService = new EmployeeServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("运行");

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        List<Employee> empAll = employeeService.getEmpAll();
        if(empAll == null){
            response.getWriter().write("抱歉，查询数据失败");
            return;
        }

        //成功查询了数据
        //存储到域对象中
        request.setAttribute("employees",empAll);
//        request.getSession().setAttribute(); session

        //跳转到显示数据的页面
        request.getRequestDispatcher("/emp_list.jsp").forward(request,response);



    }
}
